﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Eis51.CampanyCMS.FrameWork;
using System.Collections;
using Eis51.CampanyCMS.Business.NewsManager;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Model;

namespace Eis51.Web.dataPage
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            LoadNews();
        }

        private void LoadNews()
        {
            //h2Title.InnerText = Request.QueryString["name"];

            NewsQuery query = new NewsQuery();
            query.Licd = (Licd)AppContext.LCID;

            IList it = NewsBLL.GetInstance().Search(query);

            lsData.DataSource = it;
            lsData.DataBind();
            DataPager1.Visible = (it.Count > DataPager1.PageSize);
        }

        protected void LsHotelList_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            LoadNews();
        }

        protected void LsHotelList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }

        protected void DataPager1_PreRender(object sender, EventArgs e)
        {
            DataPager pager = (DataPager)sender;
            pager.Controls.Clear();
            pager.Attributes.Add("class", "pages");
            int count = pager.TotalRowCount;
            int pageSize = pager.PageSize;
            int pagesCount = count / pageSize + (count % pageSize == 0 ? 0 : 1);
            int pageSelected = pager.StartRowIndex / pageSize + 1;
            string pret = string.Format("<A title='第一页' href=\"{0}\">第一页</a> &nbsp;&nbsp;", GetUrl(1));
            string next = string.Format("&nbsp; &nbsp;<A title='尾页' href=\"{0}\">尾页</a> ", GetUrl(pagesCount));
            if (pagesCount > 1)
            {
                if (pageSelected == 1)
                {
                    //pret = "";
                }
                if (pageSelected == pagesCount)
                {
                    //next = "";
                }
                string Res = "";

                for (int i = 1; i <= pagesCount; ++i)
                {
                    Res += GetPageLink(i, pageSelected);
                }
                Literal ltt = new Literal();
                ltt.Text = pret + Res + next;
                pager.Controls.Add(ltt);
            }
        }
        private string GetUrl(int page)
        {
            string url = "NewsList.aspx";
            foreach (string key in this.Request.QueryString.AllKeys)
            {
                url = UrlUtility.AddOrReplaceParam(url, key, Request.QueryString[key], true);
            }
            url = UrlUtility.AddOrReplaceParam(url, "p", page.ToString(), true);
            return url;
        }
        private string GetPageLink(int page, int pageSelected)
        {
            string startstr = "";
            if (pageSelected != page)
            {
                startstr += string.Format("<a title=\"第{0}页\" href=\"{1}\">[{0}]</a>&nbsp;&nbsp;", page, GetUrl(page));
            }
            else
            {
                startstr += string.Format("&nbsp;&nbsp;<strong>[{0}]</strong> ", page);
            }
            return startstr;
        }

    }
}
